package com.ruoyi.receipt.utils;

import com.ruoyi.common.constant.RedisConstants;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class CompOutSubjectUtils {

    @Autowired
    private RedisCache redisCache;

    /* 定义差异化分值 */
    public Double d_score=10000D;

    /**
     * 根据用户id和题簇id,计算出用户题目id
     *
     * @param userId 用户id
     * @param clusteId 题簇id
     * @return
     */
    public Long compSingSubject(Long userId,Long clusteId){

        //定义用户已做过的题簇-题目记录本->即用户记录本
        String HAVE_COMP_CLUSTE_SUBJECT_STR=RedisConstants.HAVE_COMP_CLUSTE_SUBJECT+clusteId+":"+userId;

        //定义合成的临时题簇-题目记录本->即合成记录本
        String TEMP_HAVE_COMP_CLUSTE_SUBJECT_STR="temp:"+RedisConstants.HAVE_COMP_CLUSTE_SUBJECT+clusteId+":"+userId;

        //系统中的总记录本
        String CLUSTE_SUBJEC_RELATION_STR=RedisConstants.CLUSTE_SUBJEC_RELATION+clusteId;

        //1.(总的题簇+个人已出的题簇)->合并减去d_score的题
        redisCache.zSetunionAndStore(HAVE_COMP_CLUSTE_SUBJECT_STR,CLUSTE_SUBJEC_RELATION_STR,TEMP_HAVE_COMP_CLUSTE_SUBJECT_STR);
       //删除大于差异化分值的题
        redisCache.removeRangeByScore(TEMP_HAVE_COMP_CLUSTE_SUBJECT_STR,d_score,Integer.MAX_VALUE);

        //2.然后从中随机取一题
        Set<Long> subjectIdList = redisCache.zSetByReversePage(TEMP_HAVE_COMP_CLUSTE_SUBJECT_STR, 0, 1);

        //计算出结果
        Long resultSubjectId=null;

        if(subjectIdList.size()==0){

            //清空用户记录本
            redisCache.removeRangeByScore(HAVE_COMP_CLUSTE_SUBJECT_STR,-1,Integer.MAX_VALUE);

            redisCache.zSetunionAndStore(HAVE_COMP_CLUSTE_SUBJECT_STR,CLUSTE_SUBJEC_RELATION_STR,TEMP_HAVE_COMP_CLUSTE_SUBJECT_STR);
            //删除大于差异化分值的题
            redisCache.removeRangeByScore(TEMP_HAVE_COMP_CLUSTE_SUBJECT_STR,d_score,Integer.MAX_VALUE);

            //2.然后从中随机取一题
            subjectIdList = redisCache.zSetByReversePage(TEMP_HAVE_COMP_CLUSTE_SUBJECT_STR, 0, 1);


        }

            Iterator it = subjectIdList.iterator();

            //得出结果
            for (Iterator iterator = subjectIdList.iterator(); iterator.hasNext();) {
                resultSubjectId = (Long) iterator.next();
            }

        java.util.Random random=new java.util.Random();

        //3.回填用户记录本
        redisCache.zSetIncrScore(HAVE_COMP_CLUSTE_SUBJECT_STR,resultSubjectId,d_score+random.nextInt(100));

        return resultSubjectId;

    }

    /**
     * 计算出用户应该做的例题
     *
     * @param userId 用户id
     * @param clusteIdList 题簇顺序不能重复
     * @param number 出题的数量
     * @return
     */
    public List<Long> compOutSubject(Long userId,List<Long> clusteIdList,Integer number) {

        List<Long> resultSubjectId=new ArrayList<>();

        //对题簇进行乱序
        Collections.shuffle(clusteIdList);

        Integer count=0;

        List<Long> resultList=new ArrayList<>();

        for (int i = 0; i < number; i++) {
            if(count+1>clusteIdList.size()){
                count=0;
            }
            resultList.add(clusteIdList.get(count));
            count++;
        }

        for (int i = 0; i < resultList.size(); i++) {
            resultSubjectId.add(compSingSubject(userId,resultList.get(i)));
        }

        return resultSubjectId;

    }

}
